package com.jxwifi.cloud.quickclean.order;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.basicactivity.BasicActivity;
import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.ProductInfoVoBean;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.AuzHttp;
import com.jxwifi.cloud.quickclean.okhttp.DataJson_Cb;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.utils.TitleClient;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ProductDetailsActivity extends ClientBasicActivity {

    private String ProductCode;
    private String ProductPriceCode;
    @Bind(R.id.wv_product_details)
    WebView mWvProductDetails;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_details);
        ButterKnife.bind(this);
        openEventBus();
        //mProduct=(ProductInfoVoBean)getIntent().getExtras().getSerializable("product_details");
        ProductCode=getIntent().getStringExtra("product_code");
        ProductPriceCode=getIntent().getStringExtra("product_price_code");
        initHead();
        initData();
    }

    private void initHead() {
        TitleClient holder = new TitleClient(ButterKnife.findById(this, R.id.lin_client_title));
        holder.defineTitle("产品详情");
        holder.defineLeft(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                onBackPressed();
            }
        });
    }

    public void initData(){
        Params params = new Params();
        params.add("productCode", ProductCode);
        params.add("productPriceCode", ProductPriceCode);
        AuzHttp.post(UrlPool.POST_PRODUCT_DETAILS, params, new DataJson_Cb() {
            @Override
            public void onSuccess(int code, String data) {
                ProductInfoVoBean mBean = JSON.parseObject(data, ProductInfoVoBean.class);
                initView(mBean);
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
            }
        },TAG);
    }

    public void initView(ProductInfoVoBean mBean){
        mWvProductDetails.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        WebSettings webSettings=mWvProductDetails.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        String DetailContent=mBean.getDetailContent().replace("width=","").replace("<img","<img style=\"width:100%;height:auto;margin:0;\"");
        mWvProductDetails.loadDataWithBaseURL(null, DetailContent, "text/html", "UTF-8", null);
    }
}
